package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostGachaTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 神魔卡_抽将任务
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostGachaHeroTaskDrawReward, classtype = ActiveMessage.ActGhostGachaHeroTaskDrawRewardRequest.class)
public class CMD_Active_神魔卡_抽将任务_DrawReward extends Command<ActiveMessage.ActGhostGachaHeroTaskDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_抽将任务_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroTaskDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int id = request.getId();
		ActiveMessage.ActGhostGachaHeroTaskDrawRewardResponse.Builder response = ActiveMessage.ActGhostGachaHeroTaskDrawRewardResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.getGhostmap().containsKey(id)) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		GhostGachaTask ggt = am.getGhostmap().get(id);
		String taskcode = ggt.getTaskcode();
		Task taskbean = Task.get(playercode, taskcode);
		if(taskbean == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(taskbean.isReward()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		if(!taskbean.isFinish()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}

		task taskdata = DataCenter.getData(ggt.getTaskid(), task.class);
		if(taskdata == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(taskdata.getRewardtype(), taskdata.getRewardid(), taskdata.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		
		response.addAllDisplay(items);
		
		taskbean.setReward(true);
		taskbean.set();
		
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_神魔卡, curs);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroTaskDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
